//
//  ZYTopView.swift
//  weibo007
//
//  Created by 陈宗毅 on 15/12/20.
//  Copyright © 2015年 czy. All rights reserved.
//

import UIKit

class ZYTopView: UIView {

    //顶部视图显示内容 需要模型
    var status:ZYStatus?{
        didSet{
            //当cell给顶部视图设计模型时，顶部视图根据模型来显示数据
            //1.显示头像
            if let profile_image_url = status?.user?.profile_image_url{
              let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage:UIImage(named: "avatar"))
            }
            
            //2.用户名称
            nameLabel.text = status?.user?.screen_name
            
            //3.时间
            timeLabel.text = status?.created_at
            
            //4.来源
            sourceLabel.text = status?.source
            
            
            //5.会员等级
            //模型里面只提供mbrank或者verified_type,外面调用的时候需要判断，根据不同的值设置不同的图片，如果其他地方 使用  又要重复判断，最好的做法，是让模型直接提供对应的图片，外使用比较方便，尤其是多处地方使用的时候
            //根据不同的等级显示不同的图片
//            if  let mbrank = status?.user?.mbrank {
//                if mbrank > 0 && mbrank <= 6 {
//                  
//                    vipView.image  = UIImage(named: "")
//                }else {
//                    vipView.image = nil
//                }
//            }
            

            vipView.image = status?.user?.mbrankImage
            
            //认证图标
            //根据不同的认证类型，显示不同的认证图标
//            if let verified_type = status?.user?.verified_type{
//                //判断
//                switch verified_type {
//                case 0:
//                    verifiedView.image = UIImage(named: "")
//                case 2,3,5:
//                    verifiedView.image = UIImage(named: "")
//                case 220:
//                    verifiedView.image  = UIImage(named: "")
//                default:
//                    verifiedView.image = nil
//                }
//            }
            //外面调用 很简单
            verifiedView.image = status?.user?.verifiedImage
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor.redColor()
        prepareUI()
    }
    
    private func prepareUI(){
        //添加子控件
        addSubview(separatorView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipView)
        addSubview(verifiedView)
        
        
        //添加约束
        
        separatorView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 10))
        
        iconView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: separatorView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: 8, y: 8))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        //来源
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: timeLabel, size: nil, offset: CGPoint(x: 8, y: 0))
        //会员等级
        vipView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: 8, y: 0))
        //认证图标
        verifiedView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 懒加载
    ///cell的分割视图
    private lazy var separatorView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.95, alpha: 1)
        return view
        }()
    
    
    //用户头像
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar"))
    //用户名称
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        //大小
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()
    
    //微博时间

    private lazy var timeLabel:UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        //大小
        label.font = UIFont.systemFontOfSize(9)
        
        return label
        }()
    //来源
    private lazy var sourceLabel:UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        //大小
        label.font = UIFont.systemFontOfSize(9)
        
        return label
        }()
    //会员等级
    private lazy var vipView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    //认证图标 verifiedView
    private lazy var verifiedView:UIImageView = UIImageView(image:nil)

}
